package com.yellowpage.alarm.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

import com.yellowpage.alarm.data.AlarmInfoUser;
import com.yellowpage.alarm.dao.AlarmInfoUserDao;

public class AlarmInfoUserServiceImpl extends BaseServiceImpl implements AlarmInfoUserService {
	  private AlarmInfoUserDao alarmInfoUserDao;
	
	  public void setAlarmInfoUserDao(AlarmInfoUserDao alarmInfoUserDao) {
		  this.alarmInfoUserDao = alarmInfoUserDao;
	  }
	
	
	  /* White generate Method */ 
	  public AlarmInfoUser getAlarmInfoUser(Long id){
	      return alarmInfoUserDao.get(id);
	  }
	  
    public List<AlarmInfoUser> getAllAlarmInfoUser(){
        return alarmInfoUserDao.getAll();
    }
    
	public List<Map> queryAlarmInfoUserByPage(Map queryMap,
			PageRender pageRender) {
		Connection conn = baseDao.getConnection();

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("select DICT_VALUE as ALARM_TYPE,DICT_NAME as ALARM_TYPE_NAME from CA_DICTIONARY where DICT_TYPE='ALARM_TYPE' ");
		sqlAssistant.setTail("order by DICT_VALUE");
		List<Map> resultList = sqlAssistant.queryByPage(conn, queryMap,
				pageRender);

		for (Map result : resultList) {
			String alarmType = (String) result.get("ALARM_TYPE");
			sqlAssistant = baseDao
					.newQueryAssistantSQL("select b.NAME from CA_ALARMINFO_USER a,CA_USER b where a.user_id=b.user_id and ALARM_TYPE='"
							+ alarmType + "'");
			List<Map> userList = sqlAssistant.query(conn, queryMap);
			String users = "";
			for (int i = 0; i < userList.size(); i++) {
				if (i > 0)
					users += ",";
				users += userList.get(i).get("NAME");
			}
			result.put("USERS", users);
		}

		return resultList;
	}

	public void saveAlarmInfoUser(AlarmInfoUser alarmInfoUser) {
		alarmInfoUserDao.save(alarmInfoUser);
	}

	public void updateAlarmInfoUser(AlarmInfoUser alarmInfoUser) {
		alarmInfoUserDao.update(alarmInfoUser);
	}

	public void delete(List deleteList) {
		alarmInfoUserDao.remove(deleteList);
	}

	@Override
	public List<Map> getUserRelated(String alarmType, boolean isRelated) {
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("select u.user_id, u.name, u.display_name "
						+ " from CA_USER u "
						+ "  where "
						+ (isRelated ? "" : "not")
						+ " exists(select ur.ID from CA_ALARMINFO_USER ur "
						+ " where  ur.user_id = u.user_id and ur.ALARM_TYPE = '"
						+ alarmType + "')");

		List<Map> resultList = sqlAssistant
				.query(baseDao.getConnection(), null);

		return resultList;
	}

	@Override
	public void updateAlarmInfoUsers(String alarmType, List<Long> userIds) {
			baseDao.remove("delete from CA_ALARMINFO_USER where ALARM_TYPE=?",
					new Object[] { alarmType });
			List<AlarmInfoUser> alarmInfoUsers = new ArrayList<AlarmInfoUser>();
			for (Long userId : userIds) {
				AlarmInfoUser alarmInfoUser = new AlarmInfoUser(alarmType,
						userId);
				alarmInfoUsers.add(alarmInfoUser);
			}
			alarmInfoUserDao.save(alarmInfoUsers);

	}

	
}	